<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/11/11
 * Time: 19:20
 */

namespace business\elasticsearch\repositories;

use business\elasticsearch\models\IntegrateSupplierModel;
use business\elasticsearch\models\PlatformSupplierModel;
use business\elasticsearch\types\IntegrateSupplierMapConfigType;
use yii\elasticsearch\Query;

class IntegrateSupplierRepository
{
    /**
     * 创建索引
     * @return mixed
     */
    public function createIndex()
    {
        return PlatformSupplierModel::getDb()
            ->createCommand()
            ->createIndex(
                PlatformSupplierModel::index(),
                [
                    'settings' => [
                        'index' => [
                            'number_of_shards' => 5,
                            'number_of_replicas' => 1,
                            'analysis.analyzer.default.type' => 'ik_max_word',
                            'similarity.my_bm25.type' => 'BM25',
                            'similarity.my_bm25.b' => 0.25,
                        ],
                    ],
                    'mappings' => [
                        PlatformSupplierModel::type() => IntegrateSupplierMapConfigType::mapConfig()
                    ]
                ]
            );
    }

    /**
     * 索引是否存在
     * @return mixed
     */
    public function existsIndex()
    {
        return PlatformSupplierModel::getDb()
            ->createCommand()
            ->indexExists(PlatformSupplierModel::index());
    }

    /**
     * 保存记录
     * @param array $data
     * @param int $id
     */
    public function saveRecord($data = [], $id = 0)
    {
        if (!empty($data)) {
            return IntegrateSupplierModel::getDb()
                ->createCommand()
                ->insert(
                    IntegrateSupplierModel::index()
                    , IntegrateSupplierModel::type()
                    , $data
                    , $id
                );
        }

        return [];
    }

    /**
     * 更新记录
     * @param $id
     * @param array $data
     * @return mixed
     */
    public function updateRecord($id, $data = [])
    {
        return IntegrateSupplierModel::getDb()
            ->createCommand()
            ->update(
                IntegrateSupplierModel::index()
                , IntegrateSupplierModel::type()
                , $id
                , $data
            );
    }

    /**
     * 删除记录
     * @param $id
     * @return mixed
     */
    public function deleteRecord($id)
    {
        if (!empty($id)) {
            return IntegrateSupplierModel::getDb()
                ->createCommand()
                ->delete(
                    IntegrateSupplierModel::index()
                    , IntegrateSupplierModel::type()
                    , $id
                );
        }
    }

    /**
     * 查询记录
     * @param array $condition
     * @return array|bool
     */
    public function queryRecord($condition = [], $include = [])
    {
        return (new Query())
            ->source($include)
            ->from(IntegrateSupplierModel::index(), IntegrateSupplierModel::type())
            ->query($condition)
            ->createCommand(IntegrateSupplierModel::getDb())
            ->search();
    }

    /**
     * 查询供应商列表
     * @param array $condition
     * @param array $include
     * @param int $from
     * @param int $size
     * @param array $orderBy
     * @return array
     */
    public function queryList(
        $condition = [],
        $include = [],
        $from = 0,
        $size = 0,
        $highlight = [],
        $orderBy = ['indexCreatedTime' => SORT_DESC]
    ) {

        $preferenceStr = md5(json_encode($condition) . json_encode($include) . $from . $size . json_encode($orderBy));

        $query = (new Query())
            ->source($include)
            ->from(IntegrateSupplierModel::index(), IntegrateSupplierModel::type())
            ->options(['preference' => $preferenceStr])
            ->query($condition);

        !empty($highlight) && $query->highlight($highlight);
        !empty($orderBy) && $query->orderBy($orderBy);

        $query->offset($from)->limit($size);

        return [
            $query->count(),
            $query->all()
        ];
    }

    public function isExists($condition, $source, $limit)
    {
        $query = (new Query())
            ->source($source)
            ->from(IntegrateSupplierModel::index(), IntegrateSupplierModel::type())
            ->query($condition);
        $query->offset(0)->limit($limit);
        return $query->all();
    }
}
